import allure,pytest
import loguru
from loguru import logger
from api_util.api_util import *
from databases.databases import DB
data_List = []
data_ = DB("baijiacheng").get_one("select * from python;")
admin = perform_requests()
@allure.story("商品分类测试")
class TestGoods():
    @pytest.fixture(scope='function',autouse=True)
    @allure.feature("setup&teardown")
    def setup_teardown(self):
        if data_ !=None:
            pass
        else:
            DB("baijiacheng").insert('insert into python values(1,"佰嘉城");')
        yield
        DB("baijiacheng").delete("delete from python where id=1;")
    @allure.story("添加商品分类")
    def test_AddGoods(self):
        admin.reset_request_step()  # todo: 登录文件引用，默认不传是GetToken
        # todo： 创建请求方法  create_search_parts 可自定义命名。调用create_run_request方法，传入API名称、API地址、request body
        create_add_goods = \
            admin.create_run_request('post',
                "添加商品分类", "adminapi/product/category.html",{"pid":0,"cate_name":
                    "测试一下.","pic":"","big_pic":"","sort":0,"is_show":1}) \
                .validate() \
                .assert_equal("status_code", 200)\
                .assert_equal('headers."Content-Type"', "application/json; charset=utf-8")\
                .assert_equal("body.status", 200)\
                .assert_equal("body.msg", "添加分类成功!")

        # todo:   contruct_request_step  放入执行步骤，如果有多个，需要逗号隔开
        create_addGoods_step = admin.contruct_request_step(
            create_add_goods)
        # todo：  perform_requests_and_get_last_response  放入需要执行用例，同样，如果有多个，需要逗号隔开
        res = admin.perform_requests_and_get_last_response(
            [create_addGoods_step])
    @allure.feature("查询商品分类")
    def test_search_goods(self):
        admin.reset_request_step()
        search_add_goods = admin.create_run_request(
            'get',
            "查询商品分类", "adminapi/product/category?pid=&is_show=&page=1&cate_name=测试一下&limit=15"
        ) \
            .validate() \
            .assert_equal("status_code", 200) \
            .assert_equal('headers."Content-Type"', "application/json; charset=utf-8") \
            .assert_equal("body.status", 200) \
            .assert_equal("body.msg", "ok")
        search_addGoods_step = admin.contruct_request_step(
            search_add_goods)
        res = admin.perform_requests_and_get_last_response(
            [search_addGoods_step])
        id_ = res['res_data']['list'][-1]['id']
        data_List.append(id_)

    @allure.feature("删除商品分类")
    def test_remove_goods(self):
        admin.reset_request_step()
        remove_goods = \
            admin.create_run_request('delete',
                "删除商品分类", "adminapi/product/category/"+str(data_List[0])
                ) \
                .validate() \
                .assert_equal("status_code", 200) \
                .assert_equal('headers."Content-Type"', "application/json; charset=utf-8") \
                .assert_equal("body.status", 200) \
                .assert_equal("body.msg", "删除成功!")
        remove_Goods_step = admin.contruct_request_step(
            remove_goods)
        res = admin.perform_requests_and_get_last_response(
            [remove_Goods_step])


